package com.zimmur.quartz;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.zimmur.common.AppModuleEnum;
import com.zimmur.common.IDWorkFactory;
import com.zimmur.model.account.User;
import com.zimmur.model.account.UserMessage;
import com.zimmur.model.goods.GoodsOrganization;
import com.zimmur.model.order.Order;
import com.zimmur.service.account.IUserMessageService;
import com.zimmur.service.goods.IGoodsOrganizationService;
import com.zimmur.service.order.IOrderService;
import com.zimmur.tools.DateUtils;

@Component
public class ProcessOrganizationEnd {

	private Logger log = LoggerFactory.getLogger(ProcessOrganizationEnd.class);
	@Resource
	private IGoodsOrganizationService goodsOrganizationService;
	@Resource
	private IOrderService orderService;
	@Resource
	private IUserMessageService userMessageService;
	/**
	 * 每天凌晨1点，处理闪创商品结束的流程
	 */
//	@Scheduled(cron="0 0 1 * * ?")
	public void organizationEnd(){
		log.info("organizationEnd task begin");
		
			int unixTime=DateUtils.getNowUnixTime();
			//闪创商品
			List<GoodsOrganization> goodsOrganizations=goodsOrganizationService.selectGoodsOrganizatonByTime(unixTime);
			
			if (goodsOrganizations != null && goodsOrganizations.size()>0) {
				//闪创商品id列表
				List<Long> goodsIdList=new ArrayList<>();
				//消息列表
				List<UserMessage> userMessageList=new ArrayList<>();
				for(GoodsOrganization goodsOrganization:goodsOrganizations){
					goodsIdList.add(goodsOrganization.getGoodsId());
					userMessageList.clear();
					int num=goodsOrganization.getOrderNum()-goodsOrganization.getMinimum();
					List<Order> orders = orderService.selectOrderByGoodsIdAndStatus(goodsOrganization.getGoodsId(), "10");
					if(num<0){
						List<Long> orderIds=new ArrayList<>();
						if (orders!=null && orders.size()>0) {
							for(Order order:orders){
								orderIds.add(Long.parseLong(order.getOrderId()));
								//创建用户消息
								UserMessage userMessage=new UserMessage();
								userMessage.setMessageId(IDWorkFactory.getID(AppModuleEnum.UserMessage));
								userMessage.setToUserId(order.getUserId());
								userMessage.setCreateDate(unixTime);
								userMessage.setUserId(0L);
								userMessage.setMsgTitle("闪创订单消息");
								userMessage.setMsgContent("您的参闪订单["+order.getOrderId()+"]因参闪结束未达起闪量，货款将在3个工作日之内原路返还。");
								userMessage.setCategoryCode("organization");
								userMessage.setModuleId(90);
								userMessage.setMsgType("text");
								userMessage.setDataId(order.getOrderId()+"");
								userMessage.setIsRead(0);
								userMessage.setIsPush(0);
								userMessage.setMsgUrl("");
								//添加用户消息
								userMessageList.add(userMessage);
							}
							try {
								//更新订单
								orderService.updateOrderByOrderIds("40", "待退款", orderIds);
							} catch (Exception e) {
								log.error("update order error");
								log.error(e.getMessage(),e);
							}
						}		
					}else{
						for(Order order:orders){
							//创建用户消息
							UserMessage userMessage=new UserMessage();
							userMessage.setMessageId(IDWorkFactory.getID(AppModuleEnum.UserMessage));
							userMessage.setToUserId(order.getUserId());
							userMessage.setCreateDate(unixTime);
							userMessage.setUserId(0L);
							userMessage.setMsgTitle("闪创订单消息");
							userMessage.setMsgContent("恭喜！您的参闪订单["+order.getOrderId()+"]已达到起闪量，预计发货时间"+goodsOrganization.getWaitSendDay()+"天后。");
							userMessage.setCategoryCode("organization");
							userMessage.setModuleId(90);
							userMessage.setMsgType("text");
							userMessage.setDataId(order.getOrderId()+"");
							userMessage.setIsRead(0);
							userMessage.setIsPush(0);
							userMessage.setMsgUrl("");
							
							//添加用户消息
							userMessageList.add(userMessage);
						}
					}
					
						try {
							//保存用户消息
							if (userMessageList.size()>0) {
								
								userMessageService.addMessageList(userMessageList);							
							}
						} catch (Exception e) {
							log.error(userMessageList.toString());
							log.error(e.getMessage(),e);
						}
					
					
					try {
						//更新闪创商品			
						goodsOrganization.setIsFinish((short)1);
						goodsOrganization.setUpdateDate(unixTime);
						goodsOrganization.setOpUser("Back Service");
						goodsOrganizationService.updateOrganizationByKey(goodsOrganization);
					} catch (Exception e) {
						log.error(e.getMessage(),e);
					}
				}
				
				
			}
		
		
		
		log.info("organizationEnd task end");
	}
}
